//
//  DetailTToolsViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DetailTToolsViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "ToolsModel.h"
#import "ToolsView.h"
#import "DetailModel.h"
#import "DetailNewsCell.h"
#import "DetailCollectionCell.h"
#import "EquipmentDetailViewController.h"
@interface DetailTToolsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain)ToolsView *toolsView;
@property(nonatomic, retain)NSMutableArray *modelArr;
@property(nonatomic, retain)UISegmentedControl *segmentedControl;
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)UISegmentedControl *seg;

@property(nonatomic, retain)NSMutableArray *allArr;        // 全部装备
@property(nonatomic, retain)NSMutableArray *toolsArr;      // 工具装备
@property(nonatomic, retain)NSMutableArray *goldperArr;    // 工资装备
@property(nonatomic, retain)NSMutableArray *consumableArr; // 消耗品
@property(nonatomic, retain)NSMutableArray *visionArr;     // 视野饰品
@property(nonatomic, retain)NSMutableArray *fumoArr;       // 附魔装备
@property(nonatomic, retain)NSMutableArray *movementArr;   // 移动速度
@property(nonatomic, retain)NSMutableArray *manaregenArr;  // 法力回复
@property(nonatomic, retain)NSMutableArray *manaArr;       // 法力值
@property(nonatomic, retain)NSMutableArray *healthregenArr;// 生命回复
@property(nonatomic, retain)NSMutableArray *healthArr;     // 生命值
@property(nonatomic, retain)NSMutableArray *transformArr;  // 传输数组

@end

@implementation DetailTToolsViewController

- (void)dealloc
{
    
    [_toolsView release];
    [_collectionView release];
    [_seg release];
    [_allArr release];
    [_toolsArr release];
    [_goldperArr release];
    [_consumableArr release];
    [_visionArr release];
    [_fumoArr release];
    [_movementArr release];
    [_manaregenArr release];
    [_manaArr release];
    [_healthregenArr release];
    [_healthArr release];
    [_transformArr release];
    [_modelArr release];
    [super dealloc];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.modelArr = [NSMutableArray array];
        self.allArr = [NSMutableArray array];
        self.toolsArr = [NSMutableArray array];
        self.goldperArr = [NSMutableArray array];
        self.consumableArr = [NSMutableArray array];
        self.visionArr = [NSMutableArray array];
        self.fumoArr = [NSMutableArray array];
        self.movementArr = [NSMutableArray array];
        self.manaregenArr = [NSMutableArray array];
        self.manaArr = [NSMutableArray array];
        self.healthregenArr = [NSMutableArray array];
        self.healthArr = [NSMutableArray array];
        self.transformArr = [NSMutableArray array];
        
        self.title = @"装备";
    }
    
    return self;
    
}

#pragma mark 解析网络数据
- (void)ItemData
{
    // 所有装备
    AFHTTPRequestOperationManager *managerAll = [AFHTTPRequestOperationManager manager];
    [managerAll GET:@"http://lolbox.duowan.com/phone/apiZBItemList.php?tag=all&v=70&OSType=iOS7.1.1&versionName=2.2.1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {
            DetailModel *detail = [[DetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.allArr addObject:detail];
            [self.transformArr addObject:detail];
            [detail release];
            
        }
         self.seg.selectedSegmentIndex = 0;
        [self segmentedControl];
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // toolsArr 工具装备
    
    AFHTTPRequestOperationManager *managerTools = [AFHTTPRequestOperationManager manager];
    [managerTools GET:@"http://lolbox.duowan.com/phone/apiZBItemList.php?tag=Tools&v=70&OSType=iOS7.1.1&versionName=2.2.1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {
            DetailModel *detail = [[DetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.toolsArr addObject:detail];
            [detail release];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // goldperArr 工资装备
    
    AFHTTPRequestOperationManager *managergoldper = [AFHTTPRequestOperationManager manager];
    [managergoldper GET:@"http://lolbox.duowan.com/phone/apiZBItemList.php?tag=GoldPer&v=70&OSType=iOS7.1.1&versionName=2.2.1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {
            DetailModel *detail = [[DetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.goldperArr addObject:detail];
            [detail release];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // consumableArr 消耗品
    
    AFHTTPRequestOperationManager *managerconsumable = [AFHTTPRequestOperationManager manager];
    [managerconsumable GET:@"http://lolbox.duowan.com/phone/apiZBItemList.php?tag=consumable&v=70&OSType=iOS7.1.1&versionName=2.2.1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {
            DetailModel *detail = [[DetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.consumableArr addObject:detail];
            [detail release];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // visionArr 视野饰品
    AFHTTPRequestOperationManager *managervision = [AFHTTPRequestOperationManager manager];
    [managervision GET:@"http://lolbox.duowan.com/phone/apiZBItemList.php?tag=Vision&v=70&OSType=iOS7.1.1&versionName=2.2.1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {
            DetailModel *detail = [[DetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.visionArr addObject:detail];
            [detail release];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // fuomArr 附魔装备
    AFHTTPRequestOperationManager *managerfuom = [AFHTTPRequestOperationManager manager];
    [managerfuom GET:@"http://lolbox.duowan.com/phone/apiZBItemList.php?tag=fumo&v=70&OSType=iOS7.1.1&versionName=2.2.1"  parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {
            DetailModel *detail = [[DetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.fumoArr addObject:detail];
            [detail release];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // movementArr 移动速度
    AFHTTPRequestOperationManager *managermovement = [AFHTTPRequestOperationManager manager];
    [managermovement GET:@"http://lolbox.duowan.com/phone/apiZBItemList.php?tag=movement&v=70&OSType=iOS7.1.1&versionName=2.2.1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {
            DetailModel *detail = [[DetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.movementArr addObject:detail];
            [detail release];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // manaregenArr 法力回复
    AFHTTPRequestOperationManager *managermanagen = [AFHTTPRequestOperationManager manager];
    [managermanagen GET:@"http://lolbox.duowan.com/phone/apiZBItemList.php?tag=mana_regen&v=70&OSType=iOS7.1.1&versionName=2.2.1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {
            DetailModel *detail = [[DetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.manaregenArr addObject:detail];
            [detail release];
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // manaArr 法力值
    AFHTTPRequestOperationManager *managermana = [AFHTTPRequestOperationManager manager];
    [managermana GET:@"http://lolbox.duowan.com/phone/apiZBItemList.php?tag=mana&v=70&OSType=iOS7.1.1&versionName=2.2.1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {
            DetailModel *detail = [[DetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.manaArr addObject:detail];
            [detail release];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // healthregenArr 生命回复
    AFHTTPRequestOperationManager *managerhealthregen = [AFHTTPRequestOperationManager manager];
    [managerhealthregen GET:@"http://lolbox.duowan.com/phone/apiZBItemList.php?tag=health_regen&v=70&OSType=iOS7.1.1&versionName=2.2.1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {
            DetailModel *detail = [[DetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.healthregenArr addObject:detail];
            [detail release];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // healthArr 生命值
    AFHTTPRequestOperationManager *managerhealth = [AFHTTPRequestOperationManager manager];
    [managerhealth GET:@"http://lolbox.duowan.com/phone/apiZBItemList.php?tag=health&v=70&OSType=iOS7.1.1&versionName=2.2.1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {
            DetailModel *detail = [[DetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.healthArr addObject:detail];
            [detail release];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
   
   
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
    [self ItemData];
    
}
// 创建视图
- (void)createView
{
    

    self.toolsView = [[ToolsView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT)];
    self.toolsView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.toolsView];
    [_toolsView release];
    
    
    // 设置collectionView
    // 创建一个layout
    UICollectionViewFlowLayout *flovLayout = [[UICollectionViewFlowLayout alloc] init];
    // 图片大小
    flovLayout.itemSize = CGSizeMake(VIEWWIDTH / 5.357, VIEWHEIGHT / 8.893);
    // 最小行列间距
    flovLayout.minimumInteritemSpacing = 1;
    flovLayout.minimumLineSpacing = (VIEWWIDTH / 5.357) / 4;
    // 设置item的边框
    flovLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5, 35, VIEWWIDTH - 10, VIEWHEIGHT - 97 - 49) collectionViewLayout:flovLayout];
    // 设置代理人
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:218 / 255.0 green:218 / 255.0 blue:218 / 255.0 alpha:1];
    [self.toolsView addSubview:self.collectionView];
    [_collectionView release];
    
    // 注册方法
    [self.collectionView registerClass:[DetailCollectionCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    // 设置滑动滑动界面以及滑动界面范围
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, 32)];
    scrollView.contentSize = CGSizeMake(VIEWWIDTH * 11 / 5, VIEWHEIGHT / 20.5);
    scrollView.backgroundColor = [UIColor blackColor];
    [self.toolsView addSubview:scrollView];
    [scrollView release];
    
    // 设置装备选择界面
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"所有装备", @"工具装备", @"工资装备", @"消耗品", @"视野饰品", @"附魔装备", @"移动速度", @"法力回复", @"法力值", @"生命回复", @"生命值"]];
    self.seg.frame = CGRectMake(0, 0, VIEWWIDTH * 11 / 5, 32);
    [self.seg addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    [scrollView addSubview:self.seg];
    [_seg release];
    
    
    
    
}
// collectionView的两个协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.transformArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailCollectionCell *detailcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    DetailModel *detailmodel = self.transformArr[indexPath.row];
    detailcell.detailName.text = detailmodel.text;
    
    NSString *png = [NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png", detailmodel.id];
    NSURL *url = [NSURL URLWithString:png];
    [detailcell.detailPng sd_setImageWithURL:url];
    
    return detailcell;
}


- (void)change:(UISegmentedControl *)sander
{   [self.transformArr removeAllObjects];
    NSInteger index = self.seg.selectedSegmentIndex;
    switch (index) {
        case 0:
            for (NSMutableDictionary *dic in self.allArr) {
                [self.transformArr addObject:dic];
            }
            [self.collectionView reloadData];
            break;
        case 1:
           
            for (NSMutableDictionary *dic in self.toolsArr) {
                [self.transformArr addObject:dic];
            }
            [self.collectionView reloadData];
            
            break;
        case 2:
            [self.transformArr removeAllObjects];
            for (NSMutableDictionary *dic in self.goldperArr) {
                [self.transformArr addObject:dic];
            }
            [self.collectionView reloadData];
            break;
        case 3:
           
            for (NSMutableDictionary *dic in self.consumableArr) {
                [self.transformArr addObject:dic];
            }
            [self.collectionView reloadData];
            
            break;
        case 4:
            
            for (NSMutableDictionary *dic in self.visionArr) {
                [self.transformArr addObject:dic];
            }
            [self.collectionView reloadData];
            
            break;
        case 5:
           
            for (NSMutableDictionary *dic in self.fumoArr) {
                [self.transformArr addObject:dic];
            }
            [self.collectionView reloadData];
            
            break;
        case 6:
      
            for (NSMutableDictionary *dic in self.movementArr) {
                [self.transformArr addObject:dic];
            }
            [self.collectionView reloadData];
            
            break;
        case 7:
           
            for (NSMutableDictionary *dic in self.manaregenArr) {
                [self.transformArr addObject:dic];
            }
            [self.collectionView reloadData];
            break;
        case 8:
            
            for (NSMutableDictionary *dic in self.manaArr) {
                [self.transformArr addObject:dic];
            }
            [self.collectionView reloadData];
            break;
        case 9:
            
            for (NSMutableDictionary *dic in self.healthregenArr) {
                [self.transformArr addObject:dic];
            }
            [self.collectionView reloadData];
            break;
        case 10:
            
            for (NSMutableDictionary *dic in self.healthArr) {
                [self.transformArr addObject:dic];
            }
            [self.collectionView reloadData];
            break;
        
        default:
            break;
    }
        
}

// 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    EquipmentDetailViewController *equipment = [[EquipmentDetailViewController alloc] init];
    DetailModel *detail = self.transformArr[indexPath.row];
    equipment.equipmentid = detail.id;
    [self.navigationController pushViewController:equipment animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
